package cn.aylog.test;

import cn.aylog.dao.OrderInfoDao;
import cn.aylog.dao.UserDao;
import cn.aylog.pojo.OrderInfo;
import cn.aylog.pojo.User;
import com.mysql.cj.Session;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import java.io.InputStream;
import java.util.Properties;
import java.util.Random;

public class Demo {

    SqlSession session = null;

    @Before
    public void init(){
        String path = "mybatis.config.xml";
        try(InputStream is = Resources.getResourceAsStream(path)) {
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
            session = ssf.openSession(false);
        }catch (Exception e) {
            System.out.println(e);
        }
    }

    @After
    public void finish() {
        session.commit();
        session.close();
    }

    @Test
    public void run1() {
        UserDao mapper = session.getMapper(UserDao.class);
        User user = mapper.selectById(1);
        System.out.println(user);
    }

    @Test
    public void run2() {
        UserDao mapper = session.getMapper(UserDao.class);
        User user = mapper.selectById(5555);
        System.out.println(user);
    }

    @Test
    public void run3() {
        OrderInfoDao mapper = session.getMapper(OrderInfoDao.class);
        OrderInfo info = mapper.selectById(1);
        UserDao userDao = session.getMapper(UserDao.class);
        User user = userDao.selectById(info.getUserId());
        info.setUser(user);
        System.out.println(info);
    }

    @Test
    public void run() {
        UserDao mapper = session.getMapper(UserDao.class);
    }

//    @Test
//    public void run4() {
//        User user;
//        Random random = new Random(System.currentTimeMillis());
//        UserDao mapper = session.getMapper(UserDao.class);
//        for(int i=1; i<=10000; i++) {
//            user = new User();
//            user.setName("王五"+i);
//            user.setAge(random.nextInt(5) + 20);
//            mapper.add(user);
//        }
//    }
//
//    @Test
//    public void run5() {
//        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
//        Properties properties = new Properties();
//        properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
//        configurer.setProperties(properties);
//        configurer.setBasePackage("cn.aylog.pojo");
//    }


}
